<?php


namespace app\home\controller;


use app\base\BaseController;
use think\facade\Db;

/**
 * Class BuyHangingBillMonth
 * @package app\home\controller
 * 采购挂帐单
 */
class BuyHangingBillMonth extends BaseController
{
    protected $table='sale_hanging_bill';
    protected $title='采购挂帐单';

    /**
     * @return \json|\think\response\View
     * @throws \think\db\exception\DbException
     * date:2024/5/8
     */
    public function index()
    {
        if (request()->isAjax()) {
            $param=$this->param;
            $where[]=['pp.status','>',-1];
            $where[]=['pp.h_type','=',1];
            if (isset($this->param['status'])&&$this->param['status']){
                $where[]=['pp.status','=',$this->param['status']];
            }
            foreach (['p_code','p_name','md_bar_code','md_name','md_brand','md_specs','username'] as $v){
                if ($v=='md_bar_code'){
                    $k='md.bar_code';
                }else{
                    $k=str_replace('_','.',$v);
                }
                if (isset($this->param[$v])&&$this->param[$v]){
                    $where[]=[$k,'like','%'.$this->param[$v].'%'];
                }
            }
            if (isset($this->param['pp_time'])&&$this->param['pp_time']){
                $bt=date('Y-m-01',strtotime($this->param['pp_time']));
                $et=date('Y-m-t',strtotime($this->param['pp_time']));
                $where[]=["orderDate",'between',[$bt,$et]];
            }
            $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
            $list = Db::name($this->table)->alias('pp')
                ->join('material_dossier md','md.id=pp.material_dossier_id')
                ->join('purveyor p','p.id=pp.punter_id')
                ->join('admin a','a.id=pp.create_user','left')
                ->where($where)
                ->field("pp.*,DATE_FORMAT(pp.orderDate, '%Y-%m') as orderDate,md.bar_code,md.name as mdName,md.unit,md.specs,p.name as pName,p.code as pCode")
                ->group("DATE_FORMAT(pp.orderDate, '%Y-%m'),pp.punter_id,pp.material_dossier_id")
//                ->group('pp.punter_id')
//                ->group('pp.material_dossier_id')
                ->paginate($rows)->each(function ($item,$index){
                    $item['dzNum']=0;
                    $ids=Db::name('sale_hanging_bill')
                        ->where('status','>',-1)
                        ->where('punter_id','=',$item['punter_id'])
                        ->where('h_type','1')
                        ->where('material_dossier_id','=',$item['material_dossier_id'])
                        ->whereMonth('orderDate',$item['orderDate'])
                        ->column('id');
                        $item['num']=Db::name($this->table)
                            ->where('status','>',-1)
                            ->where('id','in',$ids)
                            ->sum('num');
                        $item['dzNum']=Db::name('sale_reconcile')->where('status',2)
                            ->where('s_type',1)
                            ->where('sale_hanging_bill_id','in',$ids)->sum('reconcileNum');

//                    if ($item['punter_begin_id']){
//                        $item['num']=Db::name($this->table)
//                            ->where('status','>',-1)
//                            ->where('id','in',$ids)
//                            ->sum('num');
//                        $item['dzNum']=Db::name('sale_reconcile')->where('status',2)
//                            ->where('s_type',1)
//                            ->where('sale_hanging_bill_id','in',$ids)->sum('reconcileNum');
//                    }elseif($item['out_receipt_detail_id']){
//
//                        $item['num']=Db::name($this->table)->where('status','>',-1)
//                            ->where('id','in',$ids)->sum('num');
//                        $item['dzNum']=Db::name('sale_reconcile')->where('status',2)
//                            ->where('sale_hanging_bill_id','in',$ids)->sum('reconcileNum');
//                    }elseif ($item['in_receipt_detail_id']){
//
////                        $item['num']=Db::name($this->table)->where('id','in',$ids)
////                            ->where('status','>',-1)->sum('num');
//                        $item['dzNum']=Db::name('sale_reconcile')->where('status',2)
//                            ->where('sale_hanging_bill_id','in',$ids)->sum('reconcileNum');
//                    }else{
//                        $item['num']=0;
//                    }
                    return $item;
                });
            return table_assign(0, '', $list);
        } else {
            return view();
        }
    }
}